﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.EntityType;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;
using Sector7.Framework.Utility;
using System.Windows.Threading;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// PatientQueueControl.xaml 的交互逻辑
    /// </summary>
    public partial class OutPatientQueueControl : AddModeControlBase
    {
        #region Accessor
        #endregion

        #region Public Functions
        #endregion

        #region Private Functions
        /// <summary>
        /// 定时器
        /// </summary>
        private void TheradTimer()
        {
            var timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 60);   //间隔1分钟
            timer.Tick += new EventHandler(OutPatientQueueControl_Loaded);
            timer.Start();
        }

        private void OutPatientQueueControl_Loaded(object obj, EventArgs e)
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadData_DoWork);
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 加载数据工作线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadData_DoWork(object obj, System.ComponentModel.DoWorkEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new DelegateWithNothing(UpdateRegistryRecordTree));
        }

        /// <summary>
        /// 更新挂号记录树
        /// </summary>
        private void UpdateRegistryRecordTree()
        {
            trnIdle.Items.Clear();
            trnCompleted.Items.Clear();
            ///根据当前科室和当前医生
            m_registryRecord = Agent_BIZS.GetRegistryRecoredListInOneDay(DateTime.Now);

            foreach (var registryRecord in m_registryRecord)
            {
                CreateRegistryRecordNode(registryRecord);
            }
        }

        /// <summary>
        /// 根据挂号记录创建挂号信息节点
        /// </summary>
        /// <param name="registryRecord">挂号记录</param>
        /// <returns></returns>
        private TreeViewItem CreateRegistryRecordNode(M7_RegistryRecord registryRecord)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = registryRecord.Patient.FullName;
            item.Tag = registryRecord;

            if (registryRecord.Status == C_BillStatus.NEW)
            {
                trnIdle.Items.Add(item);
            }
            else if(registryRecord.Status == C_BillStatus.COMPLETED)
            {
                trnCompleted.Items.Add(item);
            }

            return item;
        }

        /// <summary>
        /// 更新病人信息节点状态
        /// </summary>
        /// <param name="citizenId"></param>
        private void SetRegistryRecordToComplete(string citizenId)
        {
            foreach(var idleItem in trnIdle.Items)
            {
                var itemTag = (idleItem as TreeViewItem).Tag;
                var registryRecord = itemTag as M7_RegistryRecord;
                if (citizenId == registryRecord.CitizenId)
                {
                    registryRecord.Status = C_BillStatus.COMPLETED;
                    //更新挂号费记录到服务器
                }
            }
        }

        #endregion

        #region Structrue
        public OutPatientQueueControl()
        {
            InitializeComponent();
            TheradTimer();
        }
        #endregion

        #region Inner Member
        private List<M7_RegistryRecord> m_registryRecord = null;
        #endregion
    }
}
